<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评论人" prop="authorName">
        <el-input
          v-model="queryParams.authorName"
          placeholder="请输入评论人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论邮箱" prop="authorEmail">
        <el-input
          v-model="queryParams.authorEmail"
          placeholder="请输入评论人邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:comments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:comments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :tree-props="{children: 'children',hasChildren: 'hasChildren'}" row-key="id" v-loading="loading" :data="commentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论者名称" align="center" prop="authorName" />
      <el-table-column label="评论者邮箱" align="center" prop="authorEmail" />
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="状态" align="center" prop="cStatus">
        <template #default="scope">
            {{ scope.row.cStatus===0?'待审核':scope.row.cStatus===1?'已审核':'已删除' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="评论时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" 
          v-if="scope.row.cStatus === 1&&scope.row.parentId === undefined" 
          icon="ChatDotRound" 
          @click="handleReply(scope.row)" 
          v-hasPermi="['system:comments:add']">回复</el-button>
          <el-button link type="primary" icon="View" @click="handleExam(scope.row)" v-hasPermi="['system:comments:edit']">审核</el-button>

          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:comments:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="commentsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章表外键" prop="postId">
          <el-input v-model="form.postId" placeholder="请输入文章表外键" />
        </el-form-item>
        <el-form-item label="评论者名称" prop="authorName">
          <el-input v-model="form.authorName" placeholder="请输入评论者名称" />
        </el-form-item>
        <el-form-item label="评论者邮箱" prop="authorEmail">
          <el-input v-model="form.authorEmail" placeholder="请输入评论者邮箱" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="父评论id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父评论id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="centerDialogVisible" title="评论内容" width="500" center>
      <span>
        {{ form.content }}
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitExam(1)">
            通过
          </el-button>
          <el-button type="danger" @click="submitExam(2)">删除</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="replyDialogVisible" :title="'回复：'+form.commAuthorName" width="500" center>
      <div class="dialog-content" style="text-align: center;">
        <el-text type="primary">
          评论内容：{{ form.commContent }}
        </el-text>
        <br><br>
        <el-input type="textarea" v-model="form.content" style="width: 400px" placeholder="回复内容" />
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitReply">
            确定
          </el-button>
          <el-button type="danger" @click="replyDialogVisible = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Comments">
import { listComments, getComments, delComments, addComments, updateComments } from "@/api/system/comments"
import { ref } from "vue"

const { proxy } = getCurrentInstance()

const commentsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const centerDialogVisible = ref(false)
const replyDialogVisible = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postId: null,
    authorName: null,
    authorEmail: null,
    content: null,
    parentId: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询评论列表 */
function getList() {
  loading.value = true
  listComments(queryParams.value).then(response => {
    commentsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    postId: null,
    authorName: null,
    authorEmail: null,
    content: null,
    parentId: null,
    createTime: null,
    cStatus: null,
    commContent: null,
    commAuthorName: null,
  }
  proxy.resetForm("commentsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加评论"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getComments(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改评论"
  })
}

/** 回复按钮操作 */
function handleReply(row){
  reset()
  form.value.parentId = row.id
  form.value.commAuthorName = row.authorName
  form.value.commContent = row.content
  form.value.postId = row.postId
  replyDialogVisible.value = true
}

/** 提交回复 */
function submitReply(){
  form.value.cStatus = 0
  form.value.authorName = 'Pointer'
  addComments(form.value).then(response => {
    proxy.$modal.msgSuccess("回复成功，正在审核中。")
    replyDialogVisible.value = false
    getList()
  })
}

/** 审核按钮操作 */
function handleExam(row) {
  reset()
  form.value.id = row.id
  form.value.content = row.content
  centerDialogVisible.value = true
}

/** 提交审核 */
function submitExam(type){
  let msg = (type === 1?'已通过':'已删除')
  form.value.cStatus = type
  if (form.value.id != null) {
    updateComments(form.value).then(response => {
      proxy.$modal.msgSuccess("操作成功："+msg)
      centerDialogVisible.value = false
      getList()
    })
  }
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["commentsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateComments(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addComments(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除评论编号为"' + _ids + '"的数据项？').then(function() {
    return delComments(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/comments/export', {
    ...queryParams.value
  }, `comments_${new Date().getTime()}.xlsx`)
}

getList()
</script>
